package com.miyu.system.service.sys.impl;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.miyu.common.utils.commonUtils.Comutils;
import com.miyu.system.dao.system.SysTableDao;
import com.miyu.system.pojo.system.SysTable;
import com.miyu.system.service.sys.SysTableService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 系统表格查询服务impl
 *
 * @author longm
 * @date 2022/10/04
 */
@Service
public class SysTableServiceImpl implements SysTableService {

    @Resource
    private SysTableDao dao;

    /**
     * 保存系统表格查询
     *
     * @param param 参数
     * @return {@link JSONObject}
     */
    @Override
    public JSONObject saveSysTable( JSONObject param) {
        System.out.println(param);
        JSONObject dictTable= param.getJSONObject("dictTable");
        SysTable dict = new SysTable();
        if(dictTable.containsKey("dictId")){
            dict.setDictId(dictTable.getString("dictId"));
        }else{
            String dictId = Comutils.getUUID();
            dict.setDictId(dictId);
        }
        JSONArray dictColum = param.getJSONArray("dictColum");
        dict.setDictTable(dictTable.getString("dictTable"));
        dict.setDictColumns(dictColum.toString());
        dict.setDictMark(dictTable.getString("dictMark"));
        dict.setDictName(dictTable.getString("dictName"));
        dict.setDictType(dictTable.getString("dictType"));
        dao.saveOrUpdateByNotNullProperties(dict);
        JSONObject rs = new JSONObject();
        rs.put("code", "1");
        rs.put("msg", "操作成功");
        return rs;
    }

    /**
     * 查询系统表格查询
     *
     * @param param 参数
     * @return {@link JSONArray}
     */
    @Override
    public JSONArray querySysTable( JSONObject param) {
        StringBuffer sql = new StringBuffer();
        sql.append(" select *  from sys_table where 1=1 ");
        JSONArray rs =  dao.queryJSONArrayBySql(sql.toString());
        System.out.println(rs);
        return rs;

    }

    /**
     * 查询系统表格列
     *
     * @param param 参数
     * @return {@link JSONArray}
     */
    public JSONArray querySysTableColumns(JSONObject param) {
        SysTable dict = JSONObject.parseObject(param.toString(), SysTable.class);
        String Columns = dict.getDictColumns();
        JSONArray rs = JSONArray.parse(Columns);
        JSONArray cells= new JSONArray();
        for(int i=0;i<rs.size();i++){
            JSONObject cel = new JSONObject();
            JSONObject col = rs.getJSONObject(i);
            cel.put("key",Comutils.caseHump(col.getString("columCode")));
            cel.put("title",col.getString("columName"));
            cells.add(cel);
        }
        return cells;
    }

    /**
     * 查询表格数据
     *
     * @param param 参数
     * @return {@link JSONArray}
     */
    @Override
    public JSONArray queryTableData(JSONObject param) {
        String tableName = param.getString("dictTable");
        StringBuffer sql = new StringBuffer();
        sql.append(" select *  from ").append(tableName);
        JSONArray rs = dao.queryJSONArrayBySql(sql.toString());
        System.out.println(rs);
        return rs;
    }
}
